export default{
	state:{
		list:[
	

		],
	selectedList:[]
	},
	actions:{
		checkedAllFn({commit,getters}){
			console.log("被调用");
			getters.checkedAll ? commit("unCheckAll") : commit("checkedAll")
		},
		//删除
		delGoodsFn({commit}){
			//发送删除请求
		uni.showModal({
			title:"确认删除吗",
			success:async ()=>{
			await this.$Request({
			url:"/api/deleteCart",
			method:"POST",
			header:{
				token:true
			},
			data:{
				goodsId:state.selectedList
			}
				})
			}
		})
			commit('delGoods')
			commit('unCheckAll')
				uni.showToast({
					title:'删除成功',
					icon:"none"
				})
		}
	},
	mutations:{
		//请求到数据复制操作
		initGetData(state,list){
			state.list=list
		},
		
		//删除
		delGoods(state){
			state.list=state.list.filter(v=>{
				return state.selectedList.indexOf(v.id) ===-1
			})
		},
		//全选
		checkedAll(state){
			
			state.selectedList=state.list.map(v=>{
				v.checked=true;
				return v.id
			})
		},
		//全不选
		unCheckAll(state){
			state.list.forEach(v=>{
				v.checked=false;
			})
			state.selectedList=[]
		},
		
		//单选
		selectedItem(state,index){
			console.log("单选",index);
			let id =state.list[index].id;
			let i=state.selectedList.indexOf(id);
			//如果selectedList已经尊在就代表他之前是选中状态
			if(i>-1){
				state.list[index].checked=false;
				return state.selectedList.splice(i,1)
			}
			//如果没有被选中，checked=true
			state.list[index].checked=true;
			state.selectedList.push(id)
		},
		//加入购物车
		addShopCart(state,goods){
			console.log('加入购物车');
			state.list.push(goods)
		}
	},
	getters:{
		//判断是否全选
		checkedAll(state){
			return state.list.length ===state.selectedList.length
		},
		//合计+结算数量
		totalCount(state){
			let total={
				pprice:0,
				num:0
			}
			
			state.list.forEach(v=>{
				//是否选中
				if(state.selectedList.indexOf(v.id)>-1){
					total.pprice+=v.pprice*v.num;
					total.num=state.selectedList.length
				}
			})
			return total
		}
		
	}
}